#coding=utf-8
import unittest
import sys
  
###########取最小数字#####


#get the digit list for one integer
#input: 19, output: [1,9]   input: 100, output:[1,0]
def get_digits(number):
    a = []
    new = number
    while True:               
        a.append(new % 10)
        new = new /10
        if new == 0:            
            a.reverse()            
            return a


def search(sortedlist, digit):
    for i in sortedlist:
        if i >= digit:
            return i
    return -1
    
def get_mindigit_above0(sortedlist):
    for i in sortedlist:
        if i > 0:
            return i
    else:
        return -1

def tonumber(digitslist):    
    sum = 0
    step = 10
    for i in digitslist:
        sum = sum*step + i
    return sum

#find the integer
def find(lists, number):
    #try: 
        #check input   
        if number < 0:
            number = -1
        bvalid = True
        for dd in lists:
            if type(dd)is int:
                if dd < 0 or dd > 9:
                    bvalid = False
                    break
            else:
                bvalid = False
        if not bvalid:
            print "parameters invalid"
            return
        
        
        #compare with number+1, firstly get the digits
        digits = get_digits(number + 1)
        
        #sort the input lists
        #sortedlist = lists[:]
        #sortedlist.sort()
        sortedlist = sorted(lists)   
          
        b = False #whether find successfully
        r = [] #result
        for i in digits:
             d = search(sortedlist, i)
             if d == -1:
                 b = False
                 break
             elif d > i:
                 r.append(d)
                 b = True
                 break
             else:
                 r.append(d)
                 if len(r) == len(digits):
                     b = True
                     break
        
        if b == False:        
            mm = get_mindigit_above0(sortedlist)
            if mm <= 0:
                return
            else:
                r = [get_mindigit_above0(sortedlist)]
                while len(r) < len(digits) + 1:
                    r.append(min(sortedlist))
        else:
            while len(r) < len(digits):
                r.append(min(sortedlist))
        
        return tonumber(r)
        
    #except:    
    #    print sys.exc_info()
    #    print "Pleas contact sanjiely@gmail.com"
    

td = [[[1,2], 99, 111],
       [[0,2],33, 200],
       [[0,2],333, 2000],
       [[0,2],1, 2],
       [[0,2],0, 2],
       [[0,2],99999999, 200000000],
       [[0,2],-1, 0],
       [[0,1,2,3,4,5,6,7,8,9],99, 100],
       [[0,1,2,3,4,5,6,7,8,9],100, 101],
       [[1],100, 111],
       [[1,1,0,0],100, 101],
       ]    


class MyTest(unittest.TestCase):
    def runTest(self):
        for l in td:
            self.assertEqual(find(l[0], l[1]), l[2])            
            print l[0], "for ", l[1], " = ", find(l[0], l[1])
    
    
    
if __name__ == '__main__':   
       
    #test()
    #print get_digits(100)
    #print find([0,2], 1)
    unittest.main();
    